package com.hk.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.ConstantPointRule;
import com.hk.commons.entity.Points;
import com.hk.commons.entity.UserExtraInfo;
import com.hk.commons.vo.app.CheckResultMsg;
import com.hk.commons.vo.app.UserPointsMsg;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IPointsFunction;
import com.hk.service.IPointsHistory;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.IPoints;
import com.hk.service.sys.ISysUser;

/**
 * 该类主要用来控制添加积分的功能，设计业务逻辑
 * 
 * @author jeff
 *
 */
@Service
public class PointsFunctionImpl extends BaseServiceImpl<Points> implements
		IPointsFunction {

	private Logger logger = LoggerFactory.getLogger(PointsFunctionImpl.class);

	@Autowired
	IPoints pointsService;

	@Autowired
	ISysUser userService;

	@Autowired
	IUserExtraInfo userExtraInfoService;

	@Autowired
	IPointsHistory pointsHistoryService;

	@Override
	public UserPointsMsg signIn(Long userId) {
		logger.info("PointsFunctionImpl -- signIn" + " userId  = " + userId);
		return this.addPoints(userId, ConstantPointRule.POINT_RULE05_SIGN_IN);
	}

	@Override
	public UserPointsMsg shareFriends(Long userId) {
		logger.info("PointsFunctionImpl -- shareFriends " + " userId  = "
				+ userId);
		return this.addPoints(userId,
				ConstantPointRule.POINT_RULE06_SHARE_FRIENDS);
	}

	@Override
	public UserPointsMsg createUser(Long userId) {
		logger.info("PointsFunctionImpl -- createUser " + " userId  = "
				+ userId);
		return this.addPoints(userId, ConstantPointRule.POINT_RULE01_ADD_USER);
	}

	@Override
	public UserPointsMsg tieCard(Long userId) {
		logger.info("PointsFunctionImpl -- first tie card " + " userId  = "
				+ userId);
		return this.addPoints(userId,
				ConstantPointRule.POINT_RULE02_ADD_TIECARD);
	}

	@Override
	public UserPointsMsg addPoints(Long userId, Integer pointsRuleType) {
		logger.info("PointsFunctionImpl -- pointsRuleType = " + pointsRuleType
				+ "  userId  = " + userId);

		UserPointsMsg resp = new UserPointsMsg();

		UserExtraInfo info = userExtraInfoService.findByUserId(userId);
		Points pointsRule = pointsService.getPoinitsByType(pointsRuleType);

		String msg = "";
		// 首先确保用户个人积分信息存在
		if (info == null) {
			info = new UserExtraInfo();
			info.setUserId(userId);
			userExtraInfoService.add(info);
		}

		// 判断活动是否有效
		CheckResultMsg checkResult = pointsService.checkActive(pointsRuleType,
				userId);
		// 添加用户积分
		if (checkResult.getCheckResult()) {
			info.addPoints(pointsRule.getPoints());
			userExtraInfoService.update(info);

			// 获取描述信息
			msg = pointsRule.getActDesc();
			logger.info("UserExtraInfoImpl -- tieCard will add points "
					+ " msg  = " + msg);
			// 添加积分流水信息
			pointsHistoryService.add(userId, pointsRuleType, msg);
			resp.setResult(true);
		} else {
			resp.setResult(false);
			String type = pointsRule.getTypeName();
			if (checkResult.getCheckResultCode() == ConstantPointRule.POINT_RULE_ERROR01) {
				msg = "获取" + type + "积分失败，活动已经结束。";
			} else if (checkResult.getCheckResultCode() == ConstantPointRule.POINT_RULE_ERROR02) {
				msg = "获取" + type + "积分失败，活动尚未开始。";
			} else if (checkResult.getCheckResultCode() == ConstantPointRule.POINT_RULE_ERROR03) {
				msg = "获取" + type + "积分失败，每天最多可以获得"
						+ pointsRule.getMaxDayTimes() + "次积分。";
			}else if (checkResult.getCheckResultCode() == ConstantPointRule.POINT_RULE_ERROR04) {
				msg = "用户积分不足。";
			}

		}

		resp.setMsg(msg);
		resp.setTotalPoints(info.getTotalPoints().toString());
		resp.setUsedPoints(info.getUsedPoints().toString());
		Integer leftPoints = info.getTotalPoints() - info.getUsedPoints();
		resp.setLeftPoints(leftPoints.toString());
		return resp;
	}

}
